﻿using System;
using System.Data;
using vHCS.HR.Entities;
using System.Data.Common;
using Core.NTier.DataAccess;
using Core.Configs;
using Core.Common.Session;
using Core.Validation;
using Core.ExceptionHandling;
using Core.NTier.Entities;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace K2CoreLib.Dao.Hcs
{
    class EmployeeFilterDao : BaseDataAccess<HR_UserSeachDTO>
    {
        public EmployeeFilterDao()
            : base(CoreConfigManager.ConnectionString){ }

        public DataTable SearchEmpployeesOfHrbpOrAdmin(HR_UserSeachDTO dto, long p_CalledFunctionId, bool usertrue)
        {
            try
            {
                using (DbCommand cmd = accessor.GetStoredProcCommand("K2_spSearchEmployeeOfHrbpOrAdmin"))
                {
                    base.AssignParameters(cmd, dto);

                    accessor.AddInParameter(cmd, "LanguageId", DbType.String);
                    accessor.SetParameterValue(cmd, "LanguageId", SessionManager.LanguageID);

                    //Dành cho phân quyền dữ liệu
                    accessor.AddInParameter(cmd, "UserLogin", DbType.String);
                    accessor.SetParameterValue(cmd, "UserLogin", SessionManager.UserDomain);

                    accessor.AddInParameter(cmd, "UserDelegate", DbType.String);
                    accessor.SetParameterValue(cmd, "UserDelegate", SessionManager.UserDelegate);

                    accessor.AddInParameter(cmd, "MasterFunctionId", DbType.Int64);
                    accessor.SetParameterValue(cmd, "MasterFunctionId", p_CalledFunctionId);

                    accessor.AddInParameter(cmd, "UserTrue", DbType.Boolean);
                    accessor.SetParameterValue(cmd, "UserTrue", SafeInParameter.SafeDbValue(usertrue));

                    DataSet ds = accessor.ExecuteDataSet(cmd);
                    if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                DataAccessExceptionHandler.HandleException(ex);
            }
            return null;
        }

        ///<summary>
        /// Lấy danh sách employee theo phân quyền admin CC và HRBP
        ///</summary>
        public IEnumerable<DropDownListItem> GetEmployeesOfHrbpOrAdmin(string prefix, bool getTop)
        {
            using (DbCommand cmd = accessor.GetStoredProcCommand("K2_spGetEmployeesOfHrbpOrAdmin"))
            {
                int numOfRow = 0;
                if (getTop) numOfRow = CoreConfigManager.NumberRowOfCombo;

                accessor.AddInParameter(cmd, "Prefix", DbType.String); accessor.SetParameterValue(cmd, "Prefix", prefix);
                accessor.AddInParameter(cmd, "LanguageId", DbType.String); accessor.SetParameterValue(cmd, "LanguageId", SessionManager.LanguageID);
                accessor.AddInParameter(cmd, "NumOfRow", DbType.Int32); accessor.SetParameterValue(cmd, "NumOfRow", numOfRow);
                accessor.AddInParameter(cmd, "UserLogin", DbType.String); accessor.SetParameterValue(cmd, "UserLogin", SessionManager.UserDomain);

                return accessor.ExecuteSprocCommand<DropDownListItem>(cmd);
            }
        }
    }
}
